// @ts-check

import LocalStorageBaseTool from "./LocalStorageBaseTool.js";
import { defaultGlobalIgnoreItemConf } from "utils/localstorage/classes/localStorageIgnoreList/defaultGlobalIgnoreList.js";

/**
 * 临时储存对象的键值对定义类
 * @class TempStoreClass
 */
class TempStoreClass {
  constructor(keyname = "", value = {}) {
    this.keyname = keyname;
    this.value = value;
  }
}

/**
 * 拓展了忽略删除能力的本地缓存处理工具
 * @export
 * @class LocalStorageIgnoreList
 * @classdesc
 * 如果不想使用全局忽略的配置，可以自行实例化一个对象，自行填写需要忽略缓存名称
 * @extends {LocalStorageBaseTool}
 */
export default class LocalStorageIgnoreList extends LocalStorageBaseTool {
  constructor(ignoreList = defaultGlobalIgnoreItemConf) {
    super();
    this.ignoreList = ignoreList;
  }

  /**
   * @desc
   * 原理：临时保存 删除后再添加
   */
  clear() {
    const tempStoreList = this.ignoreList.map(
      (conf) =>
        // @ts-ignore 未知的配置预设值 允许作为取值失败时的默认值。
        new TempStoreClass(conf.key, this.get(conf.key, conf.defaultValue))
    );

    super.clear();

    tempStoreList.forEach((tempStoreObj) => {
      this.save(tempStoreObj.keyname, tempStoreObj.value);
    });
  }
}
